<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\User as UserModel;

class User extends Controller
{
    protected $UserModel;

    public function __construct()
    {
        if (!session('adminId')) {
            $this->redirect('login/index');
        }
        $this->UserModel = new UserModel();
    }

    public function index()
    {
        $UserRes = $this->UserModel->getLst();

        return view('index', [
            'user' => $UserRes,
        ]);
    }

    public function edit($id)
    {
        $userRes = $this->UserModel->get($id);
        if (request()->isPost()) {
            $data = [
                'name' => input('name'),
                'phone' => input('phone'),
            ];
            $res = $userRes->save($data);
            if ($res) {
                $this->success('编辑成功', 'user/index');
            } else {
                $this->error('编辑失败,请检查重试');
            }
        }

        return view('edit', [
            'user' => $userRes,
        ]);
    }

    public function close($id)
    {
        $userRes = $this->UserModel->get($id);
        if (request()->isPost()) {
            $data = [
                'is_close' => input('is_close'),
            ];
            $res = $userRes->save($data);
            if ($res) {
                return 1;
            } else {
                return 0;
            }
        }

        return view();
    }

    public function del($id)
    {
        if (request()->isPost()) {
            $res = $this->UserModel->destroy($id);
            if ($res) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}
